package com.egao.gps.utils;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.utils.HttpsUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.math.BigDecimal;

/**
 * @author Damon
 * @create 2020-12-28 15:42
 */
public class DistanceUtil{


    // 椭球的长半轴
    private static double EARTH_RADIUS = 6378.137;

    /**
     * 计算弧度
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度计算AB两点间的距离
     * A点经度:longitude1、A点纬度:latitude1、
     * B点经度:longitude2、  B点纬度:latitude2
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        double radLat1 = rad(latitude1);
        double radLat2 = rad(latitude2);
        double a = radLat1 - radLat2;
        double b = rad(longitude1) - rad(longitude2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        distance = distance * EARTH_RADIUS;
        BigDecimal decimal = new BigDecimal(distance*1000);
        //结果保留2位小数
        distance = decimal.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
        return distance; // {distance=700.0, units=km}
    }

    public static void main(String[] args) throws Exception{
        // A点 117.034727 36.665777	B点 117.041006 36.665871
//        double m = DistanceUtil.getDistance(121.584848,29.813381, 121.588711,29.814033);
//        System.out.println(m);//AB直线700米左右

        double[] GD = DistanceUtil.toGD("121.11972,27.887");

        double[] TX = DistanceUtil.toTX("27.887,121.11972");

        System.out.println("GD:"+GD[0]+","+ GD[1]);
        System.out.println("TX:"+TX[1]+","+ TX[0]);
    }

    //84 to 高德
    public static double[] toGD(String gps) throws Exception {
//        String ret = HttpsUtils.get("https://restapi.amap.com/v3/assistant/coordinate/convert?key=d60bb37439be2b4a4ac186cf8bb085d2&locations=120.530174,27.78359");
        String ret = HttpsUtils.get("https://restapi.amap.com/v3/assistant/coordinate/convert?key=d60bb37439be2b4a4ac186cf8bb085d2&locations="+gps);
        JSONObject ob = JSONObject.fromObject(ret);
        String[] gps2 = ob.getString("locations").split(",");
        return new double[]{Double.valueOf(gps2[0]), Double.valueOf(gps2[1])};
    }

    //84 to 腾讯
    //https://lbs.qq.com/service/webService/webServiceGuide/webServiceTranslate
    public static double[] toTX(String gps) throws Exception {
//        String ret = HttpsUtils.get("https://restapi.amap.com/v3/assistant/coordinate/convert?key=d60bb37439be2b4a4ac186cf8bb085d2&locations=120.530174,27.78359");
        String ret = HttpsUtils.get("https://apis.map.qq.com/ws/coord/v1/translate?key="+ UserConstant.TX_MAP_KEY +"&locations="+gps+"&type=1");
        JSONObject ob = JSONObject.fromObject(ret);
        JSONArray ja = ob.getJSONArray("locations");
        ob = ja.getJSONObject(0);
        String lng = ob.getString("lng");
        String lat = ob.getString("lat");
        return new double[]{Double.valueOf(lng), Double.valueOf(lat)};
    }

}
